package com.pengpeng.flash.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengpeng.flash.common.CustomException;
import com.pengpeng.flash.dto.DishDto;
import com.pengpeng.flash.entity.Dish;
import com.pengpeng.flash.entity.DishFlavor;
import com.pengpeng.flash.mapper.DishMapper;
import com.pengpeng.flash.service.DishFlavorService;
import com.pengpeng.flash.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void dishWishFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //insert插入后，会将MybatisPlus由雪花算法自动生成的主键id保存到对象中
        Long id = dishDto.getId();

        //设置每一个菜品口味对应的菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor : flavors){
            dishFlavor.setDishId(id);
        }

        //保存菜品口味
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询相应的菜品和口味用于回显到修改页面
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWishFlavor(String id) {
        Dish dish = this.getById(id);
        //把菜品的普通属性拷贝到扩展的DishDto内
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //根据菜品id查询对应的口味集合
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        //将口味集合赋值
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    /**
     * 修改菜品，同时修改相应的口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWishFlavor(DishDto dishDto) {
        //修改菜品基本信息
        this.updateById(dishDto);

        //移除之前菜品的口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //设置每一个口味的菜品ID
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors){
            flavor.setDishId(dishDto.getId());
        }
        //保存口味
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id删除对应菜品，并删除对应口味,删除之前先判断其售卖状态
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavor(String ids) {
        //将前端发送过来的ids以，分隔并放到list集合中
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        //循环遍历每一个id对应的菜品
        for (String id : strings){
            Dish dish = this.getById(id);
            //若售卖状态为‘在售’，则删除失败
            if (dish.getStatus() != 0){
                //抛出一个自定义业务异常
                throw new CustomException("菜品还在售卖中，请先停售菜品...");
            }
        }
        //删除菜品
        this.removeByIds(strings);
        //删除口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId,strings);
        dishFlavorService.remove(queryWrapper);
    }

    /**
     * 单项-批量起售停售
     * @param status
     * @param ids
     */
    @Override
    @Transactional
    public void updateStatus(Integer status,String ids) {
        //按照 ， 分隔id
        String[] split = ids.split(",");
        //循环遍历每一个ID
        for (String id :split){
            //根据id获取相应菜品，修改其状态，再保存
            Dish dish = this.getById(id);
            dish.setStatus(status);
            this.updateById(dish);
        }

    }
}
